import time
import datetime
import logging
import subprocess
import socket
from coin.support.feed_archive.app.rename_feed_tmp_file import rename_tmp_files

# sync up by date is more faster than sync all
cmd_tml = 'aws s3 sync /data/feed-data/{hostname}/{date}/{exchange} ' \
      's3://feed.raw.coin2.corp.prestolabs.io/data/flow/{hostname}/{date}/{exchange} ' \
      '--exclude="**/_*" --exclude="_*" --no-progress'

priority_sync_mea=['spot_binance_v1', 'futures_binance_v1', 'spot_okex_v5', 'futures_okex_v5-swap', 'spot_bybit_v3', 'futures_bybit_v3-linear', 'spot_coingecko_v3']

# let log file in coin folder
logging.basicConfig(filename='../coin2_sync.log', format='%(asctime)s - %(message)s', level=logging.INFO)

def sync(date_str, exchange_str=''):
  cmd = cmd_tml.format(hostname=socket.gethostname(), date=date_str, exchange=exchange_str)
  process = subprocess.Popen(
    cmd,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    shell=True,
    encoding='utf-8',
    errors='replace',
    bufsize=1)
  while True:
    realtime_output = process.stdout.readline()
    if realtime_output == '' and process.poll() is not None:
        break
    if realtime_output:
       logging.info(realtime_output.strip())


while True:
  logging.info("start rename")
  rename_tmp_files()
  today = datetime.date.today()
  yesterday = datetime.date.today() - datetime.timedelta(days=1)
  today_str = today.strftime('%Y%m%d')
  yesterday_str = yesterday.strftime('%Y%m%d')
  for mea in priority_sync_mea:
    logging.info("start sync " + today_str + "_" + mea)
    sync(today_str,mea)
  logging.info("start sync " + yesterday_str)
  sync(yesterday_str)
  logging.info("start sync " + today_str)
  sync(today_str)

  logging.info("start sleep")
  time.sleep(5 * 60)  # 5 mins
